package com.zx.crowdfunding.handler;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zx.crowdfunding.util.ResultEntity;

/**
 * Redis - Handler - 远程接口实现
 * @author 郑雪
 * @date 2021-12-29
 */
@RestController
public class RedisProviderHandler {
	
	/**
	 * Redis操作工具类（以字符串形式存入Redis缓存数据库）
	 */
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * 设置Redis键值对，并且不带超时时间
	 * @param key 	要存入redis的键
	 * @param value 要存入redis的值
	 * @return ResultEntity对象
	 */
	@RequestMapping("/set/redis/key/value/remote")
	public ResultEntity<String> setRedisKeyValueRemote(
				@RequestParam("key") String key, 
				@RequestParam("value") String value
			){
		
		try {
			ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
			
			operations.set(key, value);
			
			return ResultEntity.successWithoutData();
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
		
	}
	
	/**
	 * 设置Redis键值对，并且带超时时间
	 * @param key 		要存入redis的键
	 * @param value 	要存入redis的值
	 * @param timeout 	超时时间
	 * @param timeUnit 	时间单位
	 * @return ResultEntity对象
	 */
	@RequestMapping("/set/redis/key/value/remote/with/timeout")
	public ResultEntity<String> setRedisKeyValueRemoteWithTimeout(
				@RequestParam("key") String key, 
				@RequestParam("value") String value, 
				@RequestParam("timeout") long timeout, 
				@RequestParam("timeUnit") TimeUnit timeUnit
			){
		
		try {
			ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
			
			operations.set(key, value, timeout, timeUnit);
			
			return ResultEntity.successWithoutData();
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	/**
	 * 通过键获取Redis的字符串值
	 * @param key redis的键
	 * @return ResultEntity对象
	 */
	@RequestMapping("/get/redis/string/value/by/key/remote")
	public ResultEntity<String> getRedisStringValueByKeyRemote(@RequestParam("key") String key){
		
		try {
			ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
			
			String value = operations.get(key);
			
			return ResultEntity.successWithData(value);
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
	
	/**
	 * 删除Redis的值
	 * @param key redis的键
	 * @return ResultEntity对象
	 */
	@RequestMapping("/remove/redis/key/remote")
	public ResultEntity<String> removeRedisKeyRemote(@RequestParam("key") String key){
		try {
			stringRedisTemplate.delete(key);
			
			return ResultEntity.successWithoutData();
		} catch (Exception e) {
			e.printStackTrace();
			
			return ResultEntity.failed(e.getMessage());
		}
	}
}
